
import os
import sys
import re

g_filename_impoutput = 'ImpItemOutPut.txt';
g_filename_ralation = 'Relation.txt'
g_filename_result= 'old_friend.input'

g_oldfriend_getaward_gidlist = [];
g_relation_gidlist = [];
g_result = [];




def parse_oldfriend_getaward_gidlist():
    fp = open(g_filename_impoutput, 'r');
    for eachline in fp:
        szPatter = '(\d+)';
        m = re.match(szPatter, eachline);
        if m is not None and len(m.groups()) == 1:
            nGid = (int)(m.groups()[0]);
            g_oldfriend_getaward_gidlist.append((nGid));
    fp.close();

def parse_relation_gidlist():
    fp = open(g_filename_ralation, 'r');
    for eachline in fp:
        szPatter = '(\d+)\s+(\d+)';
        m = re.match(szPatter, eachline);
        if m is not None and len(m.groups()) == 2:
            nGid1 = (int)(m.groups()[0]);
            nGid2 = (int)(m.groups()[1]);
            g_relation_gidlist.append((nGid1,nGid2));
    fp.close();
    
def parse_result():
    #find friend recruit
    for nFrindGid in g_oldfriend_getaward_gidlist:
        for pairRelation in g_relation_gidlist:
            if nFrindGid == pairRelation[0]:
                bFind = False;
                for pariResult in g_result:
                    if pariResult[0] == pairRelation[1]:
                        bFind = True;
                        pariResult[1] = pariResult[1] + 1;
                        break;
                if not bFind:
                    g_result.append([pairRelation[1],1])

def output_result():
    fp = open(g_filename_result, 'w');
    for pairResult in g_result:
        str = "%d %d\n" % (pairResult[0],pairResult[1]);
        fp.write(str);
    fp.close();
    print 'succeed';

def main():
    parse_oldfriend_getaward_gidlist();
    parse_relation_gidlist();
    parse_result();
    output_result();

if __name__ == '__main__':
    main();